package com.sto.stoapp.activity.sales;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.sto.stoapp.activity.R;
import com.sto.stoapp.adapter.SoleAdapter;
import com.sto.stoapp.broadcast.EixtToViewManager;
import com.sto.stoapp.broadcast.HomeBroadAction;
import com.sto.stoapp.db.manager.OrderManager;
import com.sto.stoapp.entity.FunctionEntity;
import com.sto.stoapp.entity.ScanTypeEntity;
import com.sto.stoapp.listener.StoOnItemClickListener;
import com.sto.stoapp.util.Helper;
import com.sto.stoapp.widget.StoHomeActivity;

import java.util.ArrayList;
import java.util.List;

public class SalesOrderOperateActivity extends StoHomeActivity {

	private ListView OrderOperateList;
	private SoleAdapter OrderOperateAdapter;
	private String order = "";
	private Boolean OrderIsFinish = false;
	private Boolean OrderIsOper = false;
	private final int EXTRACT_RESULT = 0xfd; // 订单提取

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sto_sales_order_operate);
		this.setTitleBottom(R.string.order_piece_view,
				HomeBroadAction.SALES_MAN_BROAD);
		Bundle extras = getIntent().getExtras();
		order = extras.getString("order");
		initWidget();
		EixtToViewManager.getInstance().addActivity(this);
	}

	private void initWidget() {
		OrderOperateList = (ListView) findViewById(R.id.receivedFunc_list);
		OrderOperateAdapter = new SoleAdapter(this, getResourceList());
		OrderOperateList.setAdapter(OrderOperateAdapter);

		OrderOperateList
				.setOnItemClickListener(new StoOnItemClickListener(this) {

					@Override
					public void KcOnItemClickListener(AdapterView<?> arg0,
							View arg1, int arg2, long arg3) {
						arg1.setSelected(true);
						TextView title = (TextView) arg1
								.findViewById(R.id.res_test);
						clickItemEvent(title.getText().toString());
					}

				});

	}

	private void clickItemEvent(String title) {
		// 提取
		if (title.equals(getText(R.string.order_piece_extract))) {
			if (!OrderIsFinish) {
				Intent intent = new Intent(this, SalesSimpleRecvActivity.class);
				System.out.println("order" + order);
				intent.putExtra("busiType", ScanTypeEntity.TYPE_SALES_EXTRACT
						+ "|" + order);
				// intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				// startActivity(intent);
				startActivityForResult(intent, EXTRACT_RESULT);
			} else {
				Helper.showSoundToast(this, "该订单已完成,请返回!", false);
			}
		}
		// 打回
		else if (title.equals(getText(R.string.order_piece_back))) {
			if ((!OrderIsOper) && (!OrderIsFinish) &&(!queryOrderIsExtract(order.split("\\|")[0]))) {
				Intent intent = new Intent(this, SalesOrderDahuiActivity.class);
				System.out.println("order" + order);
				intent.putExtra("busiData", order);
				// intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent);
			} else {
				Helper.showSoundToast(this, "该订单已被提取,不能被打回!", false);
			}
		} else {

		}
	}

	private boolean queryOrderIsExtract(String order){
		return OrderManager.getInstance(this).queryOrderIsExtract(order);
		
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == EXTRACT_RESULT && resultCode == RESULT_OK) {
			// 已经提取,且完成
			OrderIsOper = true;
			OrderIsFinish = true;
			// String value = data.getStringExtra("Name");
		} else if (requestCode == EXTRACT_RESULT
				&& resultCode == RESULT_FIRST_USER) {
			// 已提取,未完成
			OrderIsOper = true;
		} else {

		}
	}

	private List<FunctionEntity> getResourceList() {
		List<String> resList = new ArrayList<String>();
		resList.add(getText(R.string.order_piece_extract).toString());
		resList.add(getText(R.string.order_piece_back).toString());

		List<FunctionEntity> functionList = new ArrayList<FunctionEntity>();
		for (String res : resList) {
			FunctionEntity entity = new FunctionEntity();
			entity.setTitle(res);
			functionList.add(entity);
		}
		return functionList;
	}


	
	@Override
	protected void onDestroy() {
		OrderOperateList = null;
		EixtToViewManager.getInstance().clearActivity();
		super.onDestroy();
	}
}
